package com.qi.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.qi.spzx.manager.service.OrderInfoService;
import com.qi.spzx.manager.service.OrderStatisticsService;
import com.qi.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2024/2/24 14:12
 * @Version 1.0.0
 */
@Component
@Slf4j
public class OrderStatisticsTask {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private OrderStatisticsService orderStatisticsService;

    //每天凌晨两点查询前一天的数据，把统计之后的数据添加到统计结果表中
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderToTalAmountStatistics(){
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));
        OrderStatistics orderStatistics = orderInfoService.selectOrderStatistics(createTime);
        if(orderStatistics != null) {
            orderStatisticsService.insert(orderStatistics) ;
        }
    }
}
